 package com.pansky.integration.common.utils;
 
 import com.google.common.collect.Lists;
 import com.google.common.collect.Maps;
 import com.google.common.collect.Sets;
 import com.pansky.integration.common.config.Global;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
 import java.util.Set;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import redis.clients.jedis.Jedis;
 import redis.clients.jedis.JedisPool;
 import redis.clients.jedis.exceptions.JedisException;
 
 public class JedisUtils
 {
   private static Logger logger = LoggerFactory.getLogger(JedisUtils.class);
 
   private static JedisPool jedisPool = (JedisPool)SpringContextHolder.getBean(JedisPool.class);
 
   public static final String KEY_PREFIX = Global.getConfig("redis.keyPrefix");
 
   public static String get(String key)
   {
     String value = null;
     Jedis jedis = null;
     try {
       jedis = getResource();
       if (jedis.exists(key).booleanValue()) {
         value = jedis.get(key);
         value = (StringUtils.isNotBlank(value)) && (!"nil".equalsIgnoreCase(value)) ? value : null;
         logger.debug("get {} = {}", key, value);
       }
     } catch (Exception e) {
       logger.warn("get {} = {}", new Object[] { key, value, e });
     } finally {
       returnResource(jedis);
     }
     return value;
   }
 
   public static Object getObject(String key)
   {
     Object value = null;
     Jedis jedis = null;
     try {
       jedis = getResource();
       if (jedis.exists(getBytesKey(key)).booleanValue()) {
         value = toObject(jedis.get(getBytesKey(key)));
         logger.debug("getObject {} = {}", key, value);
       }
     } catch (Exception e) {
       logger.warn("getObject {} = {}", new Object[] { key, value, e });
     } finally {
       returnResource(jedis);
     }
     return value;
   }
 
   public static String set(String key, String value, int cacheSeconds)
   {
     String result = null;
     Jedis jedis = null;
     try {
       jedis = getResource();
       result = jedis.set(key, value);
       if (cacheSeconds != 0) {
         jedis.expire(key, cacheSeconds);
       }
       logger.debug("set {} = {}", key, value);
     } catch (Exception e) {
       logger.warn("set {} = {}", new Object[] { key, value, e });
     } finally {
       returnResource(jedis);
     }
     return result;
   }
 
   public static String setObject(String key, Object value, int cacheSeconds)
   {
     String result = null;
     Jedis jedis = null;
     try {
       jedis = getResource();
       result = jedis.set(getBytesKey(key), toBytes(value));
       if (cacheSeconds != 0) {
         jedis.expire(key, cacheSeconds);
       }
       logger.debug("setObject {} = {}", key, value);
     } catch (Exception e) {
       logger.warn("setObject {} = {}", new Object[] { key, value, e });
     } finally {
       returnResource(jedis);
     }
     return result;
   }
 
   public static List<String> getList(String key)
   {
     List value = null;
     Jedis jedis = null;
     try {
       jedis = getResource();
       if (jedis.exists(key).booleanValue()) {
         value = jedis.lrange(key, 0L, -1L);
         logger.debug("getList {} = {}", key, value);
       }
     } catch (Exception e) {
       logger.warn("getList {} = {}", new Object[] { key, value, e });
     } finally {
       returnResource(jedis);
     }
     return value;
   }
 
   public static List<Object> getObjectList(String key)
   {
     List value = null;
     Jedis jedis = null;
     try {
       jedis = getResource();
       if (jedis.exists(getBytesKey(key)).booleanValue()) {
         List<byte[]> list = jedis.lrange(getBytesKey(key), 0L, -1L);
         value = Lists.newArrayList();
         for (byte[] bs : list) {
           value.add(toObject(bs));
         }
         logger.debug("getObjectList {} = {}", key, value);
       }
     } catch (Exception e) {
       logger.warn("getObjectList {} = {}", new Object[] { key, value, e });
     } finally {
       returnResource(jedis);
     }
     return value;
   }
 
   public static long setList(String key, List<String> value, int cacheSeconds)
   {
     long result = 0L;
     Jedis jedis = null;
     try {
       jedis = getResource();
       if (jedis.exists(key).booleanValue()) {
         jedis.del(key);
       }
       result = jedis.rpush(key, (String[])value.toArray()).longValue();
       if (cacheSeconds != 0) {
         jedis.expire(key, cacheSeconds);
       }
       logger.debug("setList {} = {}", key, value);
     } catch (Exception e) {
       logger.warn("setList {} = {}", new Object[] { key, value, e });
     } finally {
       returnResource(jedis);
     }
     return result;
   }
 
   public static long setObjectList(String key, List<Object> value, int cacheSeconds)
   {
     long result = 0L;
     Jedis jedis = null;
     try {
       jedis = getResource();
       if (jedis.exists(getBytesKey(key)).booleanValue()) {
         jedis.del(key);
       }
       List list = Lists.newArrayList();
       for (Iterator i$ = value.iterator(); i$.hasNext(); ) { Object o = i$.next();
         list.add(toBytes(o));
       }
       result = jedis.rpush(getBytesKey(key), (byte[][])list.toArray()).longValue();
       if (cacheSeconds != 0) {
         jedis.expire(key, cacheSeconds);
       }
       logger.debug("setObjectList {} = {}", key, value);
     } catch (Exception e) {
       logger.warn("setObjectList {} = {}", new Object[] { key, value, e });
     } finally {
       returnResource(jedis);
     }
     return result;
   }
 
   public static long listAdd(String key, String[] value)
   {
     long result = 0L;
     Jedis jedis = null;
     try {
       jedis = getResource();
       result = jedis.rpush(key, value).longValue();
       logger.debug("listAdd {} = {}", key, value);
     } catch (Exception e) {
       logger.warn("listAdd {} = {}", new Object[] { key, value, e });
     } finally {
       returnResource(jedis);
     }
     return result;
   }
 
   public static long listObjectAdd(String key, Object[] value)
   {
     long result = 0L;
     Jedis jedis = null;
     try {
       jedis = getResource();
       List list = Lists.newArrayList();
       for (Object o : value) {
         list.add(toBytes(o));
       }
       result = jedis.rpush(getBytesKey(key), (byte[][])list.toArray()).longValue();
       logger.debug("listObjectAdd {} = {}", key, value);
     } catch (Exception e) {
       logger.warn("listObjectAdd {} = {}", new Object[] { key, value, e });
     } finally {
       returnResource(jedis);
     }
     return result;
   }
 
   public static Set<String> getSet(String key)
   {
     Set value = null;
     Jedis jedis = null;
     try {
       jedis = getResource();
       if (jedis.exists(key).booleanValue()) {
         value = jedis.smembers(key);
         logger.debug("getSet {} = {}", key, value);
       }
     } catch (Exception e) {
       logger.warn("getSet {} = {}", new Object[] { key, value, e });
     } finally {
       returnResource(jedis);
     }
     return value;
   }
 
   public static Set<Object> getObjectSet(String key)
   {
     Set value = null;
     Jedis jedis = null;
     try {
       jedis = getResource();
       if (jedis.exists(getBytesKey(key)).booleanValue()) {
         value = Sets.newHashSet();
         Set<byte[]> set = jedis.smembers(getBytesKey(key));
         for (byte[] bs : set) {
           value.add(toObject(bs));
         }
         logger.debug("getObjectSet {} = {}", key, value);
       }
     } catch (Exception e) {
       logger.warn("getObjectSet {} = {}", new Object[] { key, value, e });
     } finally {
       returnResource(jedis);
     }
     return value;
   }
 
   public static long setSet(String key, Set<String> value, int cacheSeconds)
   {
     long result = 0L;
     Jedis jedis = null;
     try {
       jedis = getResource();
       if (jedis.exists(key).booleanValue()) {
         jedis.del(key);
       }
       result = jedis.sadd(key, (String[])value.toArray()).longValue();
       if (cacheSeconds != 0) {
         jedis.expire(key, cacheSeconds);
       }
       logger.debug("setSet {} = {}", key, value);
     } catch (Exception e) {
       logger.warn("setSet {} = {}", new Object[] { key, value, e });
     } finally {
       returnResource(jedis);
     }
     return result;
   }
 
   public static long setObjectSet(String key, Set<Object> value, int cacheSeconds)
   {
     long result = 0L;
     Jedis jedis = null;
     try {
       jedis = getResource();
       if (jedis.exists(getBytesKey(key)).booleanValue()) {
         jedis.del(key);
       }
       Set set = Sets.newHashSet();
       for (Iterator i$ = value.iterator(); i$.hasNext(); ) { Object o = i$.next();
         set.add(toBytes(o));
       }
       result = jedis.sadd(getBytesKey(key), (byte[][])set.toArray()).longValue();
       if (cacheSeconds != 0) {
         jedis.expire(key, cacheSeconds);
       }
       logger.debug("setObjectSet {} = {}", key, value);
     } catch (Exception e) {
       logger.warn("setObjectSet {} = {}", new Object[] { key, value, e });
     } finally {
       returnResource(jedis);
     }
     return result;
   }
 
   public static long setSetAdd(String key, String[] value)
   {
     long result = 0L;
     Jedis jedis = null;
     try {
       jedis = getResource();
       result = jedis.sadd(key, value).longValue();
       logger.debug("setSetAdd {} = {}", key, value);
     } catch (Exception e) {
       logger.warn("setSetAdd {} = {}", new Object[] { key, value, e });
     } finally {
       returnResource(jedis);
     }
     return result;
   }
 
   public static long setSetObjectAdd(String key, Object[] value)
   {
     long result = 0L;
     Jedis jedis = null;
     try {
       jedis = getResource();
       Set set = Sets.newHashSet();
       for (Object o : value) {
         set.add(toBytes(o));
       }
       result = jedis.rpush(getBytesKey(key), (byte[][])set.toArray()).longValue();
       logger.debug("setSetObjectAdd {} = {}", key, value);
     } catch (Exception e) {
       logger.warn("setSetObjectAdd {} = {}", new Object[] { key, value, e });
     } finally {
       returnResource(jedis);
     }
     return result;
   }
 
   public static Map<String, String> getMap(String key)
   {
     Map value = null;
     Jedis jedis = null;
     try {
       jedis = getResource();
       if (jedis.exists(key).booleanValue()) {
         value = jedis.hgetAll(key);
         logger.debug("getMap {} = {}", key, value);
       }
     } catch (Exception e) {
       logger.warn("getMap {} = {}", new Object[] { key, value, e });
     } finally {
       returnResource(jedis);
     }
     return value;
   }
 
   public static Map<String, Object> getObjectMap(String key)
   {
     Map value = null;
     Jedis jedis = null;
     try {
       jedis = getResource();
       if (jedis.exists(getBytesKey(key)).booleanValue()) {
         value = Maps.newHashMap();
         Map<byte[], byte[]> map = jedis.hgetAll(getBytesKey(key));
         for (Map.Entry e : map.entrySet()) {
           value.put(StringUtils.toString((byte[])e.getKey()), toObject((byte[])e.getValue()));
         }
         logger.debug("getObjectMap {} = {}", key, value);
       }
     } catch (Exception e) {
       logger.warn("getObjectMap {} = {}", new Object[] { key, value, e });
     } finally {
       returnResource(jedis);
     }
     return value;
   }
 
   public static String setMap(String key, Map<String, String> value, int cacheSeconds)
   {
     String result = null;
     Jedis jedis = null;
     try {
       jedis = getResource();
       if (jedis.exists(key).booleanValue()) {
         jedis.del(key);
       }
       result = jedis.hmset(key, value);
       if (cacheSeconds != 0) {
         jedis.expire(key, cacheSeconds);
       }
       logger.debug("setMap {} = {}", key, value);
     } catch (Exception e) {
       logger.warn("setMap {} = {}", new Object[] { key, value, e });
     } finally {
       returnResource(jedis);
     }
     return result;
   }
 
   public static String setObjectMap(String key, Map<String, Object> value, int cacheSeconds)
   {
     String result = null;
     Jedis jedis = null;
     try {
       jedis = getResource();
       if (jedis.exists(getBytesKey(key)).booleanValue()) {
         jedis.del(key);
       }
       Map map = Maps.newHashMap();
       for (Map.Entry e : value.entrySet()) {
         map.put(getBytesKey(e.getKey()), toBytes(e.getValue()));
       }
       result = jedis.hmset(getBytesKey(key), map);
       if (cacheSeconds != 0) {
         jedis.expire(key, cacheSeconds);
       }
       logger.debug("setObjectMap {} = {}", key, value);
     } catch (Exception e) {
       logger.warn("setObjectMap {} = {}", new Object[] { key, value, e });
     } finally {
       returnResource(jedis);
     }
     return result;
   }
 
   public static String mapPut(String key, Map<String, String> value)
   {
     String result = null;
     Jedis jedis = null;
     try {
       jedis = getResource();
       result = jedis.hmset(key, value);
       logger.debug("mapPut {} = {}", key, value);
     } catch (Exception e) {
       logger.warn("mapPut {} = {}", new Object[] { key, value, e });
     } finally {
       returnResource(jedis);
     }
     return result;
   }
 
   public static String mapObjectPut(String key, Map<String, Object> value)
   {
     String result = null;
     Jedis jedis = null;
     try {
       jedis = getResource();
       Map map = Maps.newHashMap();
       for (Map.Entry e : value.entrySet()) {
         map.put(getBytesKey(e.getKey()), toBytes(e.getValue()));
       }
       result = jedis.hmset(getBytesKey(key), map);
       logger.debug("mapObjectPut {} = {}", key, value);
     } catch (Exception e) {
       logger.warn("mapObjectPut {} = {}", new Object[] { key, value, e });
     } finally {
       returnResource(jedis);
     }
     return result;
   }
 
   public static long mapRemove(String key, String mapKey)
   {
     long result = 0L;
     Jedis jedis = null;
     try {
       jedis = getResource();
       result = jedis.hdel(key, new String[] { mapKey }).longValue();
       logger.debug("mapRemove {}  {}", key, mapKey);
     } catch (Exception e) {
       logger.warn("mapRemove {}  {}", new Object[] { key, mapKey, e });
     } finally {
       returnResource(jedis);
     }
     return result;
   }
 
   public static long mapObjectRemove(String key, String mapKey)
   {
     long result = 0L;
     Jedis jedis = null;
     try {
       jedis = getResource();
       result = jedis.hdel(getBytesKey(key), new byte[][] { getBytesKey(mapKey) }).longValue();
       logger.debug("mapObjectRemove {}  {}", key, mapKey);
     } catch (Exception e) {
       logger.warn("mapObjectRemove {}  {}", new Object[] { key, mapKey, e });
     } finally {
       returnResource(jedis);
     }
     return result;
   }
 
   public static boolean mapExists(String key, String mapKey)
   {
     boolean result = false;
     Jedis jedis = null;
     try {
       jedis = getResource();
       result = jedis.hexists(key, mapKey).booleanValue();
       logger.debug("mapExists {}  {}", key, mapKey);
     } catch (Exception e) {
       logger.warn("mapExists {}  {}", new Object[] { key, mapKey, e });
     } finally {
       returnResource(jedis);
     }
     return result;
   }
 
   public static boolean mapObjectExists(String key, String mapKey)
   {
     boolean result = false;
     Jedis jedis = null;
     try {
       jedis = getResource();
       result = jedis.hexists(getBytesKey(key), getBytesKey(mapKey)).booleanValue();
       logger.debug("mapObjectExists {}  {}", key, mapKey);
     } catch (Exception e) {
       logger.warn("mapObjectExists {}  {}", new Object[] { key, mapKey, e });
     } finally {
       returnResource(jedis);
     }
     return result;
   }
 
   public static long del(String key)
   {
     long result = 0L;
     Jedis jedis = null;
     try {
       jedis = getResource();
       if (jedis.exists(key).booleanValue()) {
         result = jedis.del(key).longValue();
         logger.debug("del {}", key);
       } else {
         logger.debug("del {} not exists", key);
       }
     } catch (Exception e) {
       logger.warn("del {}", key, e);
     } finally {
       returnResource(jedis);
     }
     return result;
   }
 
   public static long delObject(String key)
   {
     long result = 0L;
     Jedis jedis = null;
     try {
       jedis = getResource();
       if (jedis.exists(getBytesKey(key)).booleanValue()) {
         result = jedis.del(getBytesKey(key)).longValue();
         logger.debug("delObject {}", key);
       } else {
         logger.debug("delObject {} not exists", key);
       }
     } catch (Exception e) {
       logger.warn("delObject {}", key, e);
     } finally {
       returnResource(jedis);
     }
     return result;
   }
 
   public static boolean exists(String key)
   {
     boolean result = false;
     Jedis jedis = null;
     try {
       jedis = getResource();
       result = jedis.exists(key).booleanValue();
       logger.debug("exists {}", key);
     } catch (Exception e) {
       logger.warn("exists {}", key, e);
     } finally {
       returnResource(jedis);
     }
     return result;
   }
 
   public static boolean existsObject(String key)
   {
     boolean result = false;
     Jedis jedis = null;
     try {
       jedis = getResource();
       result = jedis.exists(getBytesKey(key)).booleanValue();
       logger.debug("existsObject {}", key);
     } catch (Exception e) {
       logger.warn("existsObject {}", key, e);
     } finally {
       returnResource(jedis);
     }
     return result;
   }
 
   public static Jedis getResource()
     throws JedisException
   {
     Jedis jedis = null;
     try {
       jedis = jedisPool.getResource();
     }
     catch (JedisException e) {
       logger.warn("getResource.", e);
       returnBrokenResource(jedis);
       throw e;
     }
     return jedis;
   }
 
   public static void returnBrokenResource(Jedis jedis)
   {
     if (jedis != null)
       jedisPool.returnBrokenResource(jedis);
   }
 
   public static void returnResource(Jedis jedis)
   {
     if (jedis != null)
       jedisPool.returnResource(jedis);
   }
 
   public static byte[] getBytesKey(Object object)
   {
     if ((object instanceof String)) {
       return StringUtils.getBytes((String)object);
     }
     return ObjectUtils.serialize(object);
   }
 
   public static byte[] toBytes(Object object)
   {
     return ObjectUtils.serialize(object);
   }
 
   public static Object toObject(byte[] bytes)
   {
     return ObjectUtils.unserialize(bytes);
   }
 }




